<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>选中</title>
	</head>
	<body>
		<table border="1" cellspacing="0" cellpadding="10px" align="center">
			<tr align="center">
				<th><input type="checkbox" id="ck1" onclick="checkAll1()" /></th>
				<th>姓名</th>
				<th>性别</th>
				<th>年龄</th>
				<th>家庭住址</th>
			</tr>
			<tr align="center">
				<td><input type="checkbox" name="good" value="nan" /></td>
				<td>小红</td>
				<td>女</td>
				<td>19</td>
				<td>福建省新罗区</td>
			</tr>
			<tr align="center">
				<td><input type="checkbox" name="good" value="nan" /></td>
				<td>李四</td>
				<td>男</td>
				<td>19</td>
				<td>福建省新罗区</td>
			</tr>
			<tr align="center">
				<td><input type="checkbox" name="good" value="nan" /></td>
				<td>王五</td>
				<td>男</td>
				<td>20</td>
				<td>福建省新罗区</td>
			</tr>
			<tr align="center">
				<td><input type="checkbox" name="good" value="nan" /></td>
				<td>小明</td>
				<td>男</td>
				<td>21</td>
				<td>福建省新罗区</td>
			</tr>
			<tr align="center">
				<td><input type="checkbox" name="good" value="nv" /></td>
				<td>张三</td>
				<td>男</td>
				<td>18</td>
				<td>福建省新罗区</td>
			</tr>
			<tr align="center">
				<td></td>
				<td></td>
				<td>
					<input type="checkbox" id="ck2" onclick="checkAll2();" />全选男
					<input type="checkbox" id="ck3" onclick="checkAll3();" />全选女
				</td>
				<td></td>
				<td></td>
			</tr>
		</table>
	</body>
	<script type="text/javascript">
		var ck_id = document.getElementsByTagName('input');
		var arr = document.getElementsByName('good');
		var ck1 = document.getElementById('ck1');
		var ck2 = document.getElementById('ck2');
		var ck3 = document.getElementById('ck3');
		console.log(ck_id)
		function checkAll1() {
			for (i in arr) {
				arr[i].checked = ck1.checked; // 全选
			}
		}

		function checkAll2() {
			for (var i = 0; i < ck_id.length; i++) {
				if (ck_id[i].value == "nan") {
					arr[i].checked = ck2.checked; // 全选
				}
			}
		}

		function checkAll3() {
			for (var i = 0; i < ck_id.length; i++) {
				if (ck_id[i].value == "nv") {
					arr[i].checked = ck3.checked; // 全选
				}
			}
		}
	</script>
</html>
